package com.white.mysoundrecorder;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;

import java.util.Locale;

/**
 * Created by yutf on 2017/9/1 0001.
 */
//----^----------------------fm.top
//    | 负值
//    |
//    |
//----^----------------------baseline x,y
//    |
//--------------------------fm.bottom

public class Watch extends AbsView
{
    private Paint mOutPaintStatic, mOutPaintAnim;
    private RectF mOutPaintAnimRect;

    private Paint mCenterTextPaint;
    private RectF mCenterTextRect;
    private float mCenterTextBaseline;
    private int mCenterTextColor = Color.WHITE;
    private ValueAnimator mCenterTextBlingAnim;

    private Paint mMiniSecondPaint;
    private RectF mMiniSecondRect;
    private float mMiniSecondBaseline;

    private int outCircleColorStatic = 0x33DFDFDF;
    private int outCircleColor = 0xffff6f00;

    private CalcTimer timerCircleProgress, timerNumber;

    public Watch(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }

    private void init()
    {
        mOutPaintStatic = new Paint(Paint.ANTI_ALIAS_FLAG);
        mOutPaintStatic.setStyle(Paint.Style.STROKE);
        mOutPaintStatic.setColor(outCircleColorStatic);

        mOutPaintAnim = new Paint(Paint.ANTI_ALIAS_FLAG);
        mOutPaintAnim.setStyle(Paint.Style.STROKE);
        mOutPaintAnim.setColor(outCircleColor);
        mOutPaintAnimRect = new RectF();

        mCenterTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCenterTextPaint.setColor(mCenterTextColor);
        mMiniSecondPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

        mCenterTextRect = new RectF();
        mMiniSecondRect = new RectF();

        timerNumber = new CalcTimer(1000);
        timerCircleProgress = new CalcTimer(60, CalcTimer.TYPE_REFRESH);

        startAngle = 270;
        swipeAngle = 0.36f;

        initAnim();
    }

    private void initAnim()
    {
        int initColor = mCenterTextColor;
        int endColor = 0x00000000;
        mCenterTextBlingAnim = ValueAnimator.ofArgb(initColor, endColor);
        mCenterTextBlingAnim.setDuration(1000);
        mCenterTextBlingAnim.setRepeatMode(ValueAnimator.RESTART);
        mCenterTextBlingAnim.setRepeatCount(ValueAnimator.INFINITE);
        mCenterTextBlingAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
        {
            @Override
            public void onAnimationUpdate(ValueAnimator animation)
            {
                int color = (int) animation.getAnimatedValue();
                mCenterTextPaint.setColor(color);
                postInvalidate();
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        drawCircle(canvas);
        drawCenterText(canvas);
        drawProgress(canvas);
    }

    private float startAngle, swipeAngle;

    private void drawProgress(Canvas canvas)
    {
        canvas.drawArc(mOutPaintAnimRect, startAngle, swipeAngle, false, mOutPaintAnim);
    }

    private void drawCircle(Canvas canvas)
    {
        mOutPaintStatic.setStrokeWidth(outStrokeWidth);
        canvas.drawCircle(centerWidth, centerHeight ,centerWidth - outStrokeWidth/2, mOutPaintStatic);
        mOutPaintStatic.setStrokeWidth(3);
        canvas.drawCircle(centerWidth, centerHeight ,centerWidth - 2 * outStrokeWidth, mOutPaintStatic);
    }

    String currentTimeStr;
    private void drawCenterText(Canvas canvas)
    {
        canvas.drawText(currentTimeStr, centerWidth, mCenterTextBaseline, mCenterTextPaint);
    }

    int width, height;
    float centerWidth, centerHeight;
    float outStrokeWidth;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        centerWidth = width / 2;
        centerHeight = height / 2;
        outStrokeWidth = width / 40;

        mOutPaintStatic.setStrokeWidth(outStrokeWidth);
        mOutPaintAnim.setStrokeWidth(outStrokeWidth);
        float halfStrokeWidth = outStrokeWidth / 2;
        mOutPaintAnimRect.left = halfStrokeWidth;
        mOutPaintAnimRect.top = halfStrokeWidth;
        mOutPaintAnimRect.right = width - halfStrokeWidth;
        mOutPaintAnimRect.bottom = height - halfStrokeWidth;

        float oneQ3 = height / 3;
        mCenterTextRect.left = 0;
        mCenterTextRect.top = oneQ3;
        mCenterTextRect.right = width;
        mCenterTextRect.bottom = oneQ3 * 2;

        mCenterTextPaint.setTextAlign(Paint.Align.CENTER);
        mCenterTextPaint.setTextSize(oneQ3/4*3);
        Paint.FontMetrics fm = mCenterTextPaint.getFontMetrics();
        mCenterTextBaseline = centerHeight - fm.ascent - (fm.descent- fm.ascent)/2;
    }

    public void start()
    {
        timerNumber.start(new CalcTimer.SimpleElapseListener()
        {
            @Override
            public void onChanged(long hour, long minuet, long seconds)
            {
                currentTimeStr = String.format(Locale.CHINA, "%02d:%02d", minuet, seconds);
            }
        });
        timerCircleProgress.setThreshold(1000);
        timerCircleProgress.start(new CalcTimer.SimpleElapseListener()
        {
            @Override
            public void onChanged(long hour, long minuet, long seconds)
            {
                swipeAngle = 0.36f * seconds;
                postInvalidate();
            }
        });
    }

    public void pause()
    {
        mCenterTextBlingAnim.cancel();
        mCenterTextBlingAnim.start();
        if(timerCircleProgress != null)
        {
            timerCircleProgress.pause();
        }
        if(timerNumber != null)
        {
            timerNumber.pause();
        }
    }

    public void resume()
    {
        if(mCenterTextBlingAnim!= null)
        {
            if(mCenterTextBlingAnim.isRunning())
                mCenterTextBlingAnim.cancel();
        }
        mCenterTextPaint.setColor(mCenterTextColor);
        if(timerCircleProgress != null)
        {
            timerCircleProgress.resume();
        }
        if(timerNumber != null)
        {
            timerNumber.resume();
        }
    }

    public void stop()
    {
        if(timerCircleProgress != null)
        {
            timerCircleProgress.reset();
        }
        if(timerNumber != null)
        {
            timerNumber.reset();
        }
        currentTimeStr = "00:00";
        startAngle = 270;
        swipeAngle = 0.36f;
        invalidate();
    }

    @Override
    protected void onDetachedFromWindow()
    {
        if(timerNumber != null)
        {
            timerNumber.destroy();
        }
        if(timerCircleProgress != null)
        {
            timerCircleProgress.destroy();
        }
        if(mCenterTextBlingAnim!= null)
        {
            if(mCenterTextBlingAnim.isRunning())
                mCenterTextBlingAnim.cancel();
        }
        super.onDetachedFromWindow();
    }

    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {

            invalidate();
        }
    };

    @Override
    public int getDefaultWrapWidth()
    {
        return 200;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 200;
    }

    @Override
    public void onGlobalLayout()
    {
    }
}
